﻿using MarkDownFileSplitter.Markdown;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Text.RegularExpressions;

namespace MarkDownFileSplitter.AIDelete
{
    public partial class FindContentForm : Form
    {
        internal MarkdownFileContent CurrentContent { get; set; }
        public List<ChapterInfo> items = new List<ChapterInfo>();
        public EventHandler OnOkCick;
        public FindContentForm()
        {
            InitializeComponent();
        }

        private void FindContentForm_Load(object sender, EventArgs e)
        {
            var lines = CurrentContent.ContentLines;
            // 创建一个 TreeView 控件来显示章节树
            var treeView = this.treeView1;

            var rgs = new List<Regex>();
            // 添加匹配 "1     xx" 格式的正则表达式，其中数字和标题之间可能有多个空格
            rgs.Add(new Regex(@"^\d+\s+.*$"));

            for (var i = 0; i < rgs.Count; i++)
            {
                var r = rgs[i];
                // 遍历每一行内容
                int rowIndex = 0;
                foreach (var line in lines)
                {
                    // 检查当前行是否匹配章节标题模式
                    if (r.IsMatch(line))
                    {
                        ChapterInfo chapter = new ChapterInfo()
                        {
                            line = line,
                            rowIndex = rowIndex
                        };
                        items.Add(chapter);
                    }
                    rowIndex++;
                }
            }
            items.Sort(new ChapterInfoComp());

            List<TreeNode> parentNodes = new List<TreeNode>();
            for (var i = 0; i < items.Count; i++)
            {
                TreeNode tn = new TreeNode(items[i].line);
                tn.Tag = items[i];
                parentNodes.Add(tn);
                bool find = false;
                if (items[i].Level != 0)
                {
                    for (var j = parentNodes.Count - 2; j >= 0; j--)
                    {
                        if ((parentNodes[j].Tag as ChapterInfo).Level == items[i].Level - 1)
                        {
                            parentNodes[j].Nodes.Add(tn);
                            find = true;
                            break;
                        }
                    }
                }
                if (find == false)
                    this.treeView1.Nodes.Add(tn);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (OnOkCick != null)
            {
                OnOkCick(null, null);
            }
            this.Close();
        }
    }

    public class ChapterInfo
    {
        public string line;
        public int rowIndex;
        private int m_level = -1;

        public int Level
        {
            get
            {
                if (m_level == -1)
                {
                    // 提取 line 前面的数字部分，去除多余空格，然后按 . 分隔获取长度
                    var match = Regex.Match(line, @"^\d+(?:\s*\.\s*\d+)*");
                    if (match.Success)
                    {
                        var numberStr = match.Value.Replace(" ", "");
                        m_level= numberStr.Split('.').Length;
                    }
                    else
                    {
                        m_level = 0;
                    }
                }
                return m_level;
            }
        }

        public string chapterNum
        {
            get
            {
                // 提取 line 前面的数字部分，去除多余空格，然后按 . 分隔获取长度
                var match = Regex.Match(line, @"^\d+(?:\s*\.\s*\d+)*");
                if (match.Success)
                {
                    var numberStr = match.Value.Replace(" ", "");
                    return numberStr;
                }
                return "";
            }
        }
    }
    class ChapterInfoComp : IComparer<ChapterInfo>
    {
        public int Compare(ChapterInfo? x, ChapterInfo? y)
        {
            return x.rowIndex.CompareTo(y.rowIndex);
        }

    }
}
